package com.always.electrical.energy.mqtt;

import com.always.electrical.energy.mqtt.res.MqttResponse;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class MqttTask<RES extends MqttResponse> {

    @JsonIgnore
    private final long createTime;

    private String cmd;

    private String method;

    // 设备id
    private String deviceId;

    // 消息id，每个消息都有唯一的mid
    private Integer mid;

    private String requestId;

    private String msgType;

    private String serviceId = "command";

    private CallBack<RES> callBack;

    public MqttTask() {
        this.createTime = System.currentTimeMillis();
    }

    public interface CallBack<RES extends MqttResponse> {

        void callBack(boolean success, RES res);

        default void fail() {
            callBack(false, null);
        }
    }

    public Integer mqttId() {
        if (mid != null) {
            return mid;
        } else {
            return Integer.valueOf(requestId);
        }
    }
}
